home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex10-1.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  6KB  |  292 lines

  1. // ex10-1.c -- Variations of isEqual()
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex10-1.c,v 3.0 90/05/15 22:44:15 kgorlen Rel $
  4.  
  5. // compile with -DSTRICT for strict equality
  6. // compile with -DKINDOF to use isKindOf() instead of isSpecies()
  7.  
  8. #ifndef KINDOF
  9. #define IS_SPECIES isSpecies
  10. #else
  11. #define IS_SPECIES isKindOf
  12. #endif
  13.  
  14. #include "Object.h"
  15.  
  16. class Fruit: public Object {
  17.     DECLARE_MEMBERS(Fruit);
  18.     float weight;       // weight in grams
  19.     float diameter;     // diameter in centimeters
  20. protected:
  21.     virtual void storer(OIOofd&) const;
  22.     virtual void storer(OIOout&) const;
  23. public:
  24.     Fruit(float w, float d) { weight = w; diameter = d; }
  25.     float w() const         { return weight; }
  26.     float d() const         { return diameter; }
  27.     bool operator==(const Fruit&) const;
  28.     virtual bool isEqual(const Object&) const;
  29.     virtual const Class* species() const;
  30. private:                // shouldNotImplement()
  31.     virtual int compare(const Object&) const;
  32.     virtual unsigned hash() const;
  33.     virtual void printOn(ostream& strm =cout) const;
  34. protected:              // shouldNotImplement()
  35.     virtual void deepenShallowCopy();
  36. };
  37.  
  38. #define THIS Fruit
  39. #define BASE Object
  40. #define BASE_CLASSES BASE::desc()
  41. #define MEMBER_CLASSES
  42. #define VIRTUAL_BASE_CLASSES
  43.  
  44. DEFINE_ABSTRACT_CLASS(Fruit,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex10-1.c,v 3.0 90/05/15 22:44:15 kgorlen Rel $",NULL,NULL);
  45.  
  46. bool Fruit::operator==(const Fruit& f) const
  47. {
  48.     return weight==f.weight && diameter==f.diameter;
  49. }
  50.  
  51. const Class* Fruit::species() const { return &classDesc; }
  52.  
  53. bool Fruit::isEqual(const Object& f) const
  54. {
  55.     return f.IS_SPECIES(classDesc) && *this==(const Fruit&)f;
  56. }
  57.  
  58. #undef THIS
  59. #undef BASE
  60. #undef BASE_CLASSES
  61. #undef MEMBER_CLASSES
  62. #undef VIRTUAL_BASE_CLASSES
  63.  
  64. class Apple: public Fruit {
  65.     DECLARE_MEMBERS(Apple);
  66. public:
  67.     enum appleVariety
  68.         { MCINTOSH, JONATHAN, REDDELICIOUS, STAYMAN };
  69. private:
  70.     appleVariety variety;
  71. protected:
  72.     virtual void storer(OIOofd&) const;
  73.     virtual void storer(OIOout&) const;
  74. public:
  75.     Apple(appleVariety v, float w, float d) : Fruit(w,d) {
  76.         variety = v;
  77.     }
  78.     bool operator==(const Apple&) const;
  79. #ifdef STRICT
  80.     virtual const Class* species() const;
  81.     virtual bool isEqual(const Object&) const;
  82. #endif
  83. };
  84.  
  85. #define THIS Apple
  86. #define BASE Fruit
  87. #define BASE_CLASSES BASE::desc()
  88. #define MEMBER_CLASSES
  89. #define VIRTUAL_BASE_CLASSES
  90.  
  91. DEFINE_CLASS(Apple,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex10-1.c,v 3.0 90/05/15 22:44:15 kgorlen Rel $",NULL,NULL);
  92.  
  93. bool Apple::operator==(const Apple& a) const
  94. {
  95.     return variety==a.variety && Fruit::operator==(a);
  96. }
  97.  
  98. #ifdef STRICT
  99. const Class* Apple::species() const { return &classDesc; }
  100.  
  101. bool Apple::isEqual(const Object& a) const
  102. {
  103.     return a.IS_SPECIES(classDesc) && *this==(const Apple&)a;
  104. }
  105. #endif
  106.  
  107. #undef THIS
  108. #undef BASE
  109. #undef BASE_CLASSES
  110. #undef MEMBER_CLASSES
  111. #undef VIRTUAL_BASE_CLASSES
  112.  
  113. class Orange: public Fruit {
  114.     DECLARE_MEMBERS(Orange);
  115. public:
  116.     enum orangeVariety { NAVAL, FLORIDA, CALIFORNIA };
  117. private:
  118.     orangeVariety variety;
  119. protected:
  120.     virtual void storer(OIOofd&) const;
  121.     virtual void storer(OIOout&) const;
  122. public:
  123.     Orange(orangeVariety v, float w, float d) : Fruit(w,d) {
  124.         variety = v;
  125.     }
  126.     bool operator==(const Orange&) const;
  127. #ifdef STRICT
  128.     virtual const Class* species() const;
  129.     virtual bool isEqual(const Object&) const;
  130. #endif
  131. };
  132.  
  133. #define THIS Orange
  134. #define BASE Fruit
  135. #define BASE_CLASSES BASE::desc()
  136. #define MEMBER_CLASSES
  137. #define VIRTUAL_BASE_CLASSES
  138.  
  139. DEFINE_CLASS(Orange,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex10-1.c,v 3.0 90/05/15 22:44:15 kgorlen Rel $",NULL,NULL);
  140.  
  141. bool Orange::operator==(const Orange& o) const
  142. {
  143.     return variety==o.variety && Fruit::operator==(o);
  144. }
  145.  
  146. #ifdef STRICT
  147. const Class* Orange::species() const { return &classDesc; }
  148.  
  149. bool Orange::isEqual(const Object& a) const
  150. {
  151.     return a.IS_SPECIES(classDesc) && *this==(const Orange&)a;
  152. }
  153. #endif
  154.  
  155. #undef THIS
  156. #undef BASE
  157. #undef BASE_CLASSES
  158. #undef MEMBER_CLASSES
  159. #undef VIRTUAL_BASE_CLASSES
  160.  
  161. main()
  162. {
  163.     Fruit f(100.0, 8.5);
  164.     Apple a(Apple::MCINTOSH, 100.0, 8.5);
  165.     Orange o(Orange::NAVAL, 100.0, 8.5);
  166. #ifdef STRICT
  167.     cout << "Strict equality, ";
  168. #else
  169.     cout << "Loose equality, ";
  170. #endif
  171. #ifndef KINDOF
  172.     cout << "isSpecies() comparability\n";
  173. #else
  174.     cout << "isKindOf() comparability\n";
  175. #endif
  176.     cout << "f.isEqual(a): " << (f.isEqual(a) ? "YES" : "NO")
  177.         << endl;
  178.     cout << "a.isEqual(f): " << (a.isEqual(f) ? "YES" : "NO")
  179.         << endl;
  180.     cout << "a.isEqual(o): " << (a.isEqual(o) ? "YES" : "NO")
  181.         << endl;
  182. }
  183.  
  184. #include "nihclIO.h"
  185.  
  186. #define BASE Object
  187.  
  188. int Fruit::compare(const Object&) const
  189. {
  190.     shouldNotImplement("compare");
  191.     return 0;
  192. }
  193.  
  194. void Fruit::deepenShallowCopy()
  195. {
  196.     shouldNotImplement("deepenShallowCopy");
  197.     return;
  198. }
  199.  
  200. unsigned Fruit::hash() const
  201. {
  202.     shouldNotImplement("hash");
  203.     return 0;
  204. }
  205.  
  206. void Fruit::printOn(ostream&) const
  207. {
  208.     shouldNotImplement("printOn");
  209.     return;
  210. }
  211.  
  212.  
  213. Fruit::Fruit(OIOin& strm)
  214.     : BASE(strm)
  215. {
  216.     strm >> weight >> diameter;
  217. }
  218.  
  219. void Fruit::storer(OIOout& strm) const
  220. {
  221.     BASE::storer(strm);
  222.     strm << weight << diameter;
  223. }
  224.  
  225. Fruit::Fruit(OIOifd& fd)
  226.     : BASE(fd)
  227. {
  228.     fd >> weight >> diameter;
  229. }
  230.  
  231. void Fruit::storer(OIOofd& fd) const
  232. {
  233.     BASE::storer(fd);
  234.     fd << weight << diameter;
  235. }
  236.  
  237. #undef BASE
  238. #define BASE Fruit
  239.  
  240. Apple::Apple(OIOin& strm)
  241.     : BASE(strm)
  242. {
  243.     strm >> (int)variety;
  244. }
  245.  
  246. void Apple::storer(OIOout& strm) const
  247. {
  248.     BASE::storer(strm);
  249.     strm << variety;
  250. }
  251.  
  252. Apple::Apple(OIOifd& fd)
  253.     : BASE(fd)
  254. {
  255.     fd >> (int)variety;
  256. }
  257.  
  258. void Apple::storer(OIOofd& fd) const
  259. {
  260.     BASE::storer(fd);
  261.     fd << variety;
  262. }
  263.  
  264. #undef BASE
  265. #define BASE Fruit
  266.  
  267. Orange::Orange(OIOin& strm)
  268.     : BASE(strm)
  269. {
  270.     strm >> (int)variety;
  271. }
  272.  
  273. void Orange::storer(OIOout& strm) const
  274. {
  275.     BASE::storer(strm);
  276.     strm << variety;
  277. }
  278.  
  279. Orange::Orange(OIOifd& fd)
  280.     : BASE(fd)
  281. {
  282.     fd >> (int)variety;
  283. }
  284.  
  285. void Orange::storer(OIOofd& fd) const
  286. {
  287.     BASE::storer(fd);
  288.     fd << variety;
  289. }
  290.  
  291. #undef BASE
  292.